package apps.softtech.fastmarket.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

	public static final int DATABASE_VERSION = 1;
	public static final String DATABASE_NAME = "database_rao_vat";

	private static final String TAG = "DatabaseHandler";
	
	//@formatter:off
	
	
	// UserInfo create statement
	private static final String CREATE_TABLE_USER_INFO = "CREATE TABLE IF NOT EXISTS "

			+ UserInfo.TABLE_NAME 
			+ "(" 
			+ UserInfo.ID					+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ UserInfo.USERID				+ " INTEGER, " 
			+ UserInfo.USERNAME 			+ " VARCHAR(100) NOT NULL, " 
			+ UserInfo.PASSWORD 			+ " VARCHAR(100), " 
			+ UserInfo.EMAIL 				+ " VARCHAR(100) NOT NULL, " 
			+ UserInfo.PHONENUMBER			+ " VARCHAR(100) NOT NULL, " 
			+ UserInfo.ADDRESS 				+ " VARCHAR(200), "
			+ UserInfo.USER_PERMISSION_ID 	+ " INTEGER NOT NULL, "
			+ UserInfo.CREATED_DATE 		+ " TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "
			+ UserInfo.RATE 				+ " FLOAT, " 	
			+ UserInfo.LONGITUDE 			+ " REAL, "
			+ UserInfo.LATITUDE 			+ " REAL, " 
			+ UserInfo.GCMID 				+ " VARCHAR(255), "
			+ UserInfo.AVATAR_PATH 			+ " VARCHAR(200), "
			+ UserInfo.ISACTIVE 			+ " INTEGER(1) )";


	// Category create statement
	private static final String CREATE_TABLE_CATEGORY = "CREATE TABLE IF NOT EXISTS "
			+ Category.TABLE_NAME + "("
			+ Category.ID					+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ Category.CATEGORY_ID 			+ " INTEGER, " 
			+ Category.CATEGORY_NAME 		+ " VARCHAR(100) DEFAULT NULL, " 
			+ Category.DESCRIPTION 			+ " VARCHAR(255) DEFAULT NULL, " 
			+ Category.PARENT_CATEGORY_ID 	+ " INTEGER, " 
			+ Category.CATEGORY_LEVEL 		+ " INTEGER )";

	// CategoryRequiredField
	private static final String CREATE_TABLE_CATEGORY_REQUIRED_FIELD = "CREATE TABLE IF NOT EXISTS "
			+ CategoryRequiredField.TABLE_NAME + "(" 
			+ CategoryRequiredField.CATEGORY_ID 	+ " INTEGER, "
			+ CategoryRequiredField.TAG_ID 			+ " INTEGER, " 
			+ " PRIMARY KEY (" + CategoryRequiredField.CATEGORY_ID + "," + CategoryRequiredField.TAG_ID + ")"
			+ " CONSTRAINT FK_CategoryRequiredFields_Category FOREIGN KEY " 
			+ " ( " + CategoryRequiredField.CATEGORY_ID + ")"
			+ " REFERENCES "+ Category.TABLE_NAME+ "("+ Category.CATEGORY_ID+ ")"
			+ " ON DELETE CASCADE ON UPDATE CASCADE, "
			+ " CONSTRAINT FK_Comment_Product FOREIGN KEY ("+ CategoryRequiredField.TAG_ID+ " )"
			+ " REFERENCES "+ Tag.TABLE_NAME+ " ("+ Tag.TAG_ID+ ")"
			+ " ON DELETE RESTRICT ON UPDATE CASCADE " + ")";

	// ChatLog create statement
	private static final String CREATE_TABLE_CHAT_LOG = "CREATE TABLE IF NOT EXISTS "
			+ ChatLog.TABLE_NAME + "("
			+ ChatLog.ID 			+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ ChatLog.CHAT_LOG_ID 	+ " INTEGER, " 
			+ ChatLog.CONTENT 		+ " TEXT, " 
			+ ChatLog.CREATED_DATE 	+ " TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " 
			+ ChatLog.FROM_USER_ID 	+ " INTEGER, " 
			+ ChatLog.TO_USER_ID 	+ " INTEGER, " 
			+ ChatLog.TYPE 			+ " INTEGER(1) )";
	// Comment create statement
	private static final String CREATE_TABLE_COMMENT = "CREATE TABLE IF NOT EXISTS "
			+ Comment.TABLE_NAME + "(" 
			+ Comment.COMMENT_ID 		+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
			+ Comment.COMMENT_CONTENT 	+ " TEXT, " 
			+ Comment.CREATED_DATE		+ " TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " 
			+ Comment.PRODUCT_ID		+ " INTEGER NOT NULL, " 
			+ Comment.USERID 			+ " INTEGER NOT NULL )";

	// FeedBack create statement
	private static final String CREATE_TABLE_FEEDBACK = "CREATE TABLE IF NOT EXISTS "
			+ FeedBack.TABLE_NAME + "(" 
			+ FeedBack.FEEDBACK_ID		+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ FeedBack.CONTENT_FEEDBACK + " NVARCHAR(100), " 
			+ FeedBack.RATE  			+ " INTEGER, " 
			+ FeedBack.CREATED_DATE		+ " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "
			+ FeedBack.USERID 			+ " INTEGER , "
			+ FeedBack.PRODUCT_ID		+ " INTEGER )";

	// followproduct create statement
	private static final String CREATE_TABLE_FOLLOW_PRODUCT = "CREATE TABLE IF NOT EXISTS "
			+ FollowProduct.TABLE_NAME + "(" 
			+ FollowProduct.FOLLOW_ID           + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
			+ FollowProduct.USERID              + " INTEGER, " 
			+ FollowProduct.PRODUCT_ID 			+ " INTEGER, "
			+ FollowProduct.FOLLOW_DATE         + " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "
			+ FollowProduct.GET_NOTIFICATION 	+ " INTEGER," 
			+ FollowProduct.UNFOLLOW		 	+ " INTEGER )";

	// followuser create statement
	private static final String CREATE_TABLE_FOLLOW_USER = "CREATE TABLE IF NOT EXISTS "
			+ FollowUser.TABLE_NAME + "(" 
			+ FollowUser.FOLLOW_ID			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
			+ FollowUser.FROM_USER_ID		+ " INTEGER, " 
			+ FollowUser.TO_USER_ID 		+ " INTEGER, "
			+ FollowUser.FOLLOW_DATE		+ " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "
			+ FollowUser.GET_NOTIFICATION 	+ " INTEGER(1), "
			+ FollowUser.UNFOLLOW		 	+ " INTEGER(1) )";

	// Offer create statement
	private static final String CREATE_TABLE_OFFER = "CREATE TABLE IF NOT EXISTS "
			+ Offer.TABLE_NAME + "(" 
			+ Offer.ID              + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ Offer.OFFER_ID		+ " INTEGER, " 
			+ Offer.USERID			+ " INTEGER, " 
			+ Offer.OFFER_PRICE		+ " FLOAT, " 
			+ Offer.PRODUCT_ID		+ " INTEGER , " 
			+ Offer.CREATED_DATE	+ " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " 
			+ Offer.ISAPPLY			+ " INTEGER NOT NULL )";

	// Product create statement
	private static final String CREATE_TABLE_PRODUCT = "CREATE TABLE IF NOT EXISTS "
			+ Product.TABLE_NAME + "(" 
			+ " ID INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ Product.PRODUCT_ID		+ " INTEGER, " 
			+ Product.USERID 			+ " INTEGER, " 
			+ Product.CATEGORY_ID 		+ " INTEGER, " 
			+ Product.CREATED_DATE 		+ " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "
			+ Product.LAST_UPDATE 		+ " TIMESTAMP, " 
			+ Product.OFFER_ID 			+ " INTEGER, "
			+ Product.DESCRIPTION 		+ " TEXT, "
			+ Product.PRODUCT_NAME 		+ " NVARCHAR(255), "
			+ Product.AVATAR_PATH 		+ " NVARCHAR(255), "
			+ Product.IMAGE1	 		+ " NVARCHAR(255), "
			+ Product.IMAGE2 			+ " NVARCHAR(255), "
			+ Product.IMAGE3	 		+ " NVARCHAR(255), "
			+ Product.PRICE 			+ " FLOAT )";

	// ProductTag create statement
	private static final String CREATE_TABLE_PRODUCT_TAG = "CREATE TABLE IF NOT EXISTS "
			+ ProductTag.TABLE_NAME + "("
			+ ProductTag.ID				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
			+ ProductTag.PRODUCT_ID 	+ " INTEGER NOT NULL, " 
			+ ProductTag.VALUE 			+ " TEXT, "
			+ ProductTag.TAG_ID 		+ " INTEGER NOT NULL, "
			+ ProductTag.VALUE_TYPE 	+ " INTEGER(1) )";

	// Report create statement
	private static final String CREATE_TABLE_REPORT = "CREATE TABLE IF NOT EXISTS "
			+ Report.TABLE_NAME + "(" 
			+ Report.REPORT_ID		+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
			+ Report.OBJECT_ID		+ " INTEGER , " 
			+ Report.OBJECT_TYPE 	+ " INTEGER , "
			+ Report.CREATED_DATE 	+ " TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "
			+ Report.USERID 		+ " INTEGER )";

	// Tag create statement
	private static final String CREATE_TABLE_TAG = "CREATE TABLE IF NOT EXISTS "
			+ Tag.TABLE_NAME + "(" 
			+ Tag.TAG_ID			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
			+ Tag.TAG_NAME			+ " NVARCHAR(100) , "
			+ Tag.TAG_DESCRIPTION 	+ " TEXT )";
	


	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		Log.i(TAG,"onCreatDatabaseHandler");
		
		db.execSQL(CREATE_TABLE_CATEGORY);
		db.execSQL(CREATE_TABLE_CATEGORY_REQUIRED_FIELD);
		db.execSQL(CREATE_TABLE_CHAT_LOG);
		db.execSQL(CREATE_TABLE_COMMENT);
		db.execSQL(CREATE_TABLE_FEEDBACK);
		db.execSQL(CREATE_TABLE_FOLLOW_PRODUCT);
		db.execSQL(CREATE_TABLE_FOLLOW_USER);
		db.execSQL(CREATE_TABLE_OFFER);
		db.execSQL(CREATE_TABLE_PRODUCT);
		db.execSQL(CREATE_TABLE_PRODUCT_TAG);
		db.execSQL(CREATE_TABLE_REPORT);
		db.execSQL(CREATE_TABLE_TAG);
		db.execSQL(CREATE_TABLE_USER_INFO);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		db.execSQL("DROP TABLE IF EXISTS " + Category.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + CategoryRequiredField.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + ChatLog.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + Comment.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + FeedBack.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + FollowProduct.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + FollowUser.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + Offer.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + Product.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + ProductTag.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + Report.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + Tag.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + UserInfo.TABLE_NAME);
		onCreate(db);
	}
//@formatter:on
	/**
	 * insert row in to category
	 * 
	 * @param category
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertCategory(Category category) {
		SQLiteDatabase db = this.getWritableDatabase();
		if (category.getCategoryId() == 0) {
			Log.e(TAG, "insert error");
			return 0;
		} else {
			ContentValues values = new ContentValues();
			values = category.getValueCategory();
			long id = db.insert(Category.TABLE_NAME, null, values);
			db.close();
			return id;
		}

	}

	/**
	 * insert row into table CategoryRequiredField
	 * 
	 * @param field
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertCategoryRequiredField(CategoryRequiredField field) {
		SQLiteDatabase db = this.getWritableDatabase();
		if (field.getCategoryId() < 0 || field.getTagId() < 0) {
			Log.i(TAG, "insert CategoryRequiredField error ");
			return -1;
		} else {
			ContentValues values = new ContentValues();
			values = field.getValueCategoryRequired();
			long id = db.insert(CategoryRequiredField.TABLE_NAME, null, values);
			db.close();
			return id;
		}

	}

	/**
	 * inserting a row into table insertChatLog.
	 * 
	 * @param chatLog
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertChatLog(ChatLog chatLog) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = chatLog.getValueChatLog();
		long id = db.insert(ChatLog.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table Comment
	 * 
	 * @param comment
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertComment(Comment comment) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = comment.getValuesComment();
		long id = db.insert(Comment.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table FeedBack
	 * 
	 * @param feedBack
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertFeedBack(FeedBack feedBack) {
		SQLiteDatabase db = this.getWritableDatabase();
		if (feedBack.getCreatedDate().equals(null)
				|| feedBack.getCreatedDate().equals("")) {
			Log.i(TAG, "insert FeedBack error");
			return -1;
		}
		ContentValues values = new ContentValues();
		values = feedBack.getValueFeedBack();
		long id = db.insert(FeedBack.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table FollowProduct
	 * 
	 * @param followProduct
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertFollowProduct(FollowProduct followProduct) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = followProduct.getValueFollowProduct();
		long id = db.insert(FollowProduct.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table FollowUser
	 * 
	 * @param followUser
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertFollowUser(FollowUser followUser) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = followUser.getValueFollowUser();
		long id = db.insert(FollowUser.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table Offer
	 * 
	 * @param offer
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertOffer(Offer offer) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = offer.getValueOffer();
		long id = db.insert(Offer.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table Product
	 * 
	 * @param offer
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertProduct(Product product) {
		SQLiteDatabase db = this.getWritableDatabase();
		if (product.getCreatedDate().equals(null)) {
			Log.e(TAG, "insert Product error");
			return -1;
		}
		ContentValues values = new ContentValues();
		values = product.getValueProduct();
		long id = db.insert(Product.TABLE_NAME, null, values);
		Log.e(TAG, "insert Product " + id);
		db.close();
		return id;
	}

	/**
	 * insert row into table ProductTag
	 * 
	 * @author MUI
	 * @param productTag
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertProductTag(ProductTag productTag) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = productTag.getValueProductTag();
		long id = db.insert(ProductTag.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table Report
	 * 
	 * @author MUI
	 * @param report
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertReport(Report report) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = report.getValueReport();
		long id = db.insert(Report.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table Tag
	 * 
	 * @author MUI
	 * @param report
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertTag(Tag tag) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = tag.getValueTag();
		long id = db.insert(Tag.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * insert row into table UserInfo
	 * 
	 * @author MUI
	 * @param report
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertUserInfo(UserInfo userInfo) {
		SQLiteDatabase db = this.getWritableDatabase();
//		Log.e(TAG, "start method insert User");
		if (userInfo.getUserName().equals(null)
				|| userInfo.getEmail().equals(null)
				|| userInfo.getEmail().equals("")
				|| userInfo.getPhoneNumber().equals(null)
				|| userInfo.getPhoneNumber().equals("")) {
			Log.e(TAG, "insert UserInfo Error");
			return -1;
		}
		ContentValues values = new ContentValues();
		values = userInfo.getValueUserInfo();
		long id = db.insert(UserInfo.TABLE_NAME, null, values);
		db.close();
		return id;
	}

	/**
	 * method to update row in Table Category
	 * 
	 * @author MUI
	 * @param category
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateCategory(Category category, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = category.getValueCategory();
		int number = db.update(Category.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table CategoryRequiredField
	 * 
	 * @author MUI
	 * @param field
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateCategoryRequired(CategoryRequiredField field,
			String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = field.getValueCategoryRequired();
		int number = db.update(CategoryRequiredField.TABLE_NAME, values,
				whereClause, whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table Comment
	 * 
	 * @author MUI
	 * @param comment
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateComment(Comment comment, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = comment.getValuesComment();
		int number = db.update(Comment.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table FeedBack
	 * 
	 * @author MUI
	 * @param feedBack
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateFeedBack(FeedBack feedBack, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = feedBack.getValueFeedBack();
		int number = db.update(FeedBack.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table FollowProduct
	 * 
	 * @author MUI
	 * @param fProduct
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateFollowProduct(FollowProduct fProduct, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = fProduct.getValueFollowProduct();
		int number = db.update(FollowProduct.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table FollowUser
	 * 
	 * @author MUI
	 * @param fUser
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateFollowUser(FollowUser fUser, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = fUser.getValueFollowUser();
		int number = db.update(FollowUser.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table FollowUser
	 * 
	 * @author MUI
	 * @param offer
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateOffer(Offer offer, String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = offer.getValueOffer();
		int number = db
				.update(Offer.TABLE_NAME, values, whereClause, whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table Product
	 * 
	 * @author MUI
	 * @param product
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateProduct(Product product, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		if (product.getCreatedDate().equals(null)) {
			Log.e(TAG, "update Product error");
			return 0;
		}
		ContentValues values = new ContentValues();
		values = product.getValueProduct();
		int number = db.update(Product.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table ProductTag
	 * 
	 * @author MUI
	 * @param productTag
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateProductTag(ProductTag productTag, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = productTag.getValueProductTag();
		int number = db.update(ProductTag.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table Report
	 * 
	 * @author MUI
	 * @param report
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateReport(Report report, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = report.getValueReport();
		int number = db.update(Report.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table Tag
	 * 
	 * @author MUI
	 * @param tag
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateTag(Tag tag, String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = tag.getValueTag();
		int number = db.update(Tag.TABLE_NAME, values, whereClause, whereArgs);
		db.close();
		return number;
	}

	/**
	 * method to update row in Table UserInfo
	 * 
	 * @author MUI
	 * @param userInfo
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateUserInfo(UserInfo userInfo, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();

		if (userInfo.getUserName().equals(null)
				|| userInfo.getPassword().equals(null)
				|| userInfo.getPassword().equals("")
				|| userInfo.getEmail().equals(null)
				|| userInfo.getEmail().equals("")
				|| userInfo.getPhoneNumber().equals(null)
				|| userInfo.getPhoneNumber().equals("")) {
			Log.e(TAG, "update UserInfo Error");
			return 0;
		}
		ContentValues values = new ContentValues();
		values = userInfo.getValueUserInfo();
		int number = db.update(UserInfo.TABLE_NAME, values, whereClause,
				whereArgs);
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table Category
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteCategory(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(Category.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table CategoryRequired
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteCategoryRequired(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(CategoryRequiredField.TABLE_NAME, whereClause,
					whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table ChatLog
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteChatLog(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(ChatLog.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table Comment
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteComment(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(Comment.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table FeedBack
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteFeedBack(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(FeedBack.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table FollowProduct
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteFollowProduct(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db
					.delete(FollowProduct.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table FollowUser
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteFollowUser(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(FollowUser.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table Offer
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteOffer(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(Offer.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table Product
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteProduct(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(Product.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table ProductTag
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteProductTag(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(ProductTag.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table Report
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteReport(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(Report.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table Tag
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteTag(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(Tag.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * method for deleting rows in table UserInfo
	 * 
	 * @author MUI
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int deleteUserInfo(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db.delete(UserInfo.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		db.close();
		return number;
	}

	/**
	 * get Category
	 * 
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<Category> appropriate with selection
	 */
	public List<Category> getCategory(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<Category> list = new ArrayList<Category>();
		Cursor cursor = db.query(Category.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				Category category = new Category();
				category.setValueFromCursor(cursor);
				list.add(category);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get CategoryRequiredField
	 * 
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<CategoryRequiredField> appropriate with selection
	 */
	public List<CategoryRequiredField> getCategoryRequiredField(
			String[] columns, String selection, String[] selectionArgs,
			String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<CategoryRequiredField> list = new ArrayList<CategoryRequiredField>();
		Cursor cursor = db.query(CategoryRequiredField.TABLE_NAME, columns,
				selection, selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				CategoryRequiredField category = new CategoryRequiredField();
				category.setValueFromCursor(cursor);
				list.add(category);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get ChatLog
	 * 
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<CategoryRequiredField> appropriate with selection
	 */
	public List<ChatLog> getChatLog(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<ChatLog> list = new ArrayList<ChatLog>();
		Cursor cursor = db.query(ChatLog.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				ChatLog chatLog = new ChatLog();
				chatLog.setValueFromCursor(cursor);
				list.add(chatLog);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get ChatLog
	 * 
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<CategoryRequiredField> appropriate with selection
	 */
	public List<ChatLog> getChatLog(Boolean distinct, String[] columns,
			String selection, String[] selectionArgs, String orderBy,
			String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<ChatLog> list = new ArrayList<ChatLog>();
		Cursor cursor = db.query(distinct, ChatLog.TABLE_NAME, columns,
				selection, selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				ChatLog chatLog = new ChatLog();
				chatLog.setValueFromCursor(cursor);
				list.add(chatLog);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get Comment
	 * 
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<CategoryRequiredField> appropriate with selection
	 */
	public List<Comment> getComment(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<Comment> list = new ArrayList<Comment>();
		Cursor cursor = db.query(Comment.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				Comment comment = new Comment();
				comment.setValueFromCursor(cursor);
				list.add(comment);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get FeedBack
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<CategoryRequiredField> appropriate with selection
	 */
	public List<FeedBack> getFeedBack(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<FeedBack> list = new ArrayList<FeedBack>();
		Cursor cursor = db.query(FeedBack.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				FeedBack feedBack = new FeedBack();
				feedBack.setValueFromCursor(cursor);
				list.add(feedBack);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get FollowProduct
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<CategoryRequiredField> appropriate with selection
	 */
	public List<FollowProduct> getFollowProduct(String[] columns,
			String selection, String[] selectionArgs, String orderBy,
			String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<FollowProduct> list = new ArrayList<FollowProduct>();
		Cursor cursor = db.query(FollowProduct.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				FollowProduct fp = new FollowProduct();
				fp.setValueFromCursor(cursor);
				list.add(fp);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get FollowUser
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<CategoryRequiredField> appropriate with selection
	 */
	public List<FollowUser> getFollowUser(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<FollowUser> list = new ArrayList<FollowUser>();
		Cursor cursor = db.query(FollowUser.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				FollowUser fp = new FollowUser();
				fp.setValueFromCursor(cursor);
				list.add(fp);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get Offer
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<Offer> appropriate with selection
	 */
	public List<Offer> getOffer(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<Offer> list = new ArrayList<Offer>();
		Cursor cursor = db.query(Offer.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				Offer offer = new Offer();
				offer.setValueFromCursor(cursor);
				list.add(offer);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get Product
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<Product> appropriate with selection
	 */
	public List<Product> getProduct(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<Product> list = new ArrayList<Product>();
		Cursor cursor = db.query(Product.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				Product product = new Product();
				product.setValueFromCursor(cursor);
				list.add(product);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get ProductTag
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<ProductTag> appropriate with selection
	 */
	public List<ProductTag> getProductTag(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<ProductTag> list = new ArrayList<ProductTag>();
		Cursor cursor = db.query(ProductTag.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				ProductTag pTag = new ProductTag();
				pTag.setValueFromCursor(cursor);
				list.add(pTag);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;
	}

	/**
	 * get Report
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<CategoryRequiredField> appropriate with selection
	 */
	public List<Report> getReport(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<Report> list = new ArrayList<Report>();
		Cursor cursor = db.query(Report.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				Report report = new Report();
				report.setValueFromCursor(cursor);
				list.add(report);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get Tag
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<Tag> appropriate with selection
	 */
	public List<Tag> getTag(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<Tag> list = new ArrayList<Tag>();
		Cursor cursor = db.query(Tag.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				Tag tag = new Tag();
				tag.setValueFromCursor(cursor);
				list.add(tag);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		db.close();
		return list;

	}

	/**
	 * get UserInfo
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<UserInfo> appropriate with selection
	 */
	public List<UserInfo> getUserInfo(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<UserInfo> list = new ArrayList<UserInfo>();
		Cursor c = db.query(UserInfo.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (c.moveToFirst()) {
			do {
				UserInfo user = new UserInfo();
				user.setValueFromCursor(c);
				list.add(user);
			} while (c.moveToNext());
		}
		if (c != null && !c.isClosed())
			c.close();
		db.close();
		return list;

	}

	public void excuteQuery(String query) {
		SQLiteDatabase db = this.getReadableDatabase();
		db.execSQL(query);
	}

	
	public List<SaleTopic> getPurchaseProduct(int userId){
		SQLiteDatabase db = this.getReadableDatabase();
		
		String sql =  "SELECT * FROM " + Product.TABLE_NAME + " , " + Offer.TABLE_NAME
				+ " WHERE " 
				+ Product.TABLE_NAME + "." + Product.OFFER_ID 
				+ " = " 
				+ Offer.TABLE_NAME + "." + Offer.OFFER_ID 
				+ " AND "
				+ Product.TABLE_NAME + "." + Offer.USERID + " = " + userId ;
		
		Cursor c = db.rawQuery(sql, null);
		List<SaleTopic> listPurchase = new ArrayList<SaleTopic>();
		
		if(c.moveToFirst()){
			do {
				SaleTopic sale = new SaleTopic();
				sale.setTopic(c.getString(c.getColumnIndex(Product.PRODUCT_NAME)));
				sale.setDescription(c.getString(c.getColumnIndex(Product.DESCRIPTION)));
				sale.setOfferPrice(c.getFloat(c.getColumnIndex(Offer.OFFER_PRICE)));
				sale.setCreatedDate(c.getString(c.getColumnIndex(Offer.CREATED_DATE)));
				listPurchase.add(sale);
			} while (c.moveToNext());
		}
		if (c != null && !c.isClosed())
			c.close();
		db.close();
		
		return listPurchase;
				
	}
	public List<SaleTopic> getSaledProduct(int userId) {
		
		SQLiteDatabase db = this.getReadableDatabase();
		
		String sql = "SELECT * FROM " + Product.TABLE_NAME + " , " + Offer.TABLE_NAME
				+ " WHERE " 
				+ Product.TABLE_NAME + "." + Product.OFFER_ID 
				+ " = " 
				+ Offer.TABLE_NAME + "." + Offer.OFFER_ID
				+ " AND "
				+ Product.TABLE_NAME + "." +Product.USERID + " = " + userId ;
		
		Cursor c = db.rawQuery(sql, null);
		List<SaleTopic> listSale = new ArrayList<SaleTopic>();
		if(c.moveToFirst()){
			do {
				SaleTopic sale = new SaleTopic();
				sale.setTopic(c.getString(c.getColumnIndex(Product.PRODUCT_NAME)));
				sale.setDescription(c.getString(c.getColumnIndex(Product.DESCRIPTION)));
				sale.setOfferPrice(c.getFloat(c.getColumnIndex(Offer.OFFER_PRICE)));
				sale.setCreatedDate(c.getString(c.getColumnIndex(Offer.CREATED_DATE)));
				listSale.add(sale);
			} while (c.moveToNext());
		}
		if (c != null && !c.isClosed())
			c.close();
		db.close();
		return listSale;
		
//		String sql = "SELECT * " 
//		+ "Product." + Product.PRODUCT_NAME  + ","
//		+ "Product." + Product.DESCRIPTION   + ", " 
//		+ "Offer."   + Offer.OFFER_PRICE     + ", " 
//		+ "Offer."   + Offer.CREATED_DATE    
//		+ " FROM (" + Product.TABLE_NAME
//		+ " INNER JOIN " + Product.TABLE_NAME + " ON " 
//		+ " Product." + Product.USERID + " = UserInfo." + UserInfo.USERID + ") "
//		+ " INNER JOIN " + Offer.TABLE_NAME + " ON " 
//		+ " Product." +Product.OFFER_ID + " = Offer." + Offer.OFFER_ID
//		+ " WHERE UserInfo." + UserInfo.USERID + " = " + userId;
	}

}
